package com.imooc.newretail.smartcabinet.ctrl;

import com.imooc.newretail.common.BaseController;
import com.imooc.newretail.common.utils.BeanUtils;
import com.imooc.newretail.common.vo.RV;
import com.imooc.newretail.common.vo.ResultVO;
import com.imooc.newretail.smartcabinet.entity.IotSmartCabinet;
import com.imooc.newretail.smartcabinet.form.DeviceFrom;
import com.imooc.newretail.smartcabinet.service.IIotSmartCabinetService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

/**
 * new-retail-teach
 * <p>
 *
 * </p>
 *
 * @author Hank
 * @since 2020-06-30
 */
@Api(value = "设备管理控制类", tags = "设备管理控制类")
@RestController
@RequestMapping(value = "/device")
public class DeviceController extends BaseController<IIotSmartCabinetService> {


    @ApiOperation(value = "注册单个设备", notes = "注册单个设备")
    @PostMapping("/register")
    public ResultVO registerDevice(@RequestBody DeviceFrom device) {
        IotSmartCabinet smartCabinet = BeanUtils.copyProperties(device, IotSmartCabinet.class);
        service.saveOrUpdate(smartCabinet);
        return RV.success(null);
    }


    @ApiOperation(value = "激活设备", notes = "激活设备")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deviceId", value = "deviceId", required = true),
            @ApiImplicitParam(name = "token", value = "token", required = true)
    })
    @PostMapping("/activate/{deviceId}/{token}")
    public ResultVO activate(@PathVariable("deviceId") String deviceId,
                             @PathVariable("token") String token) {
        IotSmartCabinet device = service.getById(deviceId);
        IotSmartCabinet smartCabinet = BeanUtils.copyProperties(device, IotSmartCabinet.class);
        service.saveOrUpdate(smartCabinet);
        return RV.success(null);
    }
}
